import React from 'react';
import * as axios from 'axios';
import orgCtrl from '@/ts/controller';
import { useHistory } from 'react-router-dom';
import cls from './index.module.less';
import AuthLayout from '../layout';
import { getQueryByKey } from '@/utils/tools';
import message from '@/utils/message';
const FOALoginContent: React.FC = () => {
  const history = useHistory();
  const tokenId = getQueryByKey('tokenid');
  if (tokenId && tokenId.length > 0) {
    axios.default
      .post('/FOALogin.out', tokenId)
      .then((res) => {
        if (res.status === 200 && res.data && res.data.success) {
          if (res.data.data && res.data.data.length > 0) {
            sessionStorage.setItem('accessToken', res.data.data);
            orgCtrl.auth.tokenAuth().then(() => {
              window.location.href = '/#/home';
            });
            return;
          }
          history.push(`/auth?tokenid=${tokenId}`);
        } else {
          message.error(`登录失败,${res.data.message},请手动登录！`);
          history.push(`/auth`);
        }
      })
      .catch(() => {
        history.push('/auth');
      });
  } else {
    history.push('/auth');
  }
  return (
    <AuthLayout>
      <div className={cls.download_body}>
        <div className={cls.title}>预算管理一体化系统认证</div>
        <div>请稍后...</div>
      </div>
    </AuthLayout>
  );
};

export default FOALoginContent;
